/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serverClasses;

import commonClasses.Messages.AckMsg;
import commonClasses.Messages.EnterAuctionMsg;
import commonClasses.Messages.NakMsg;
import commonClasses.Utils;
import commonClasses.commonInterfaces.HostModelIntf;
import commonClasses.commonInterfaces.ProtocolIntf;

/**
 *
 * @author MGL
 */
public class EnterAuctionMsgService {

    private ProtocolIntf _protocol;
    private HostModelIntf _model;
    private String _hash;

    public EnterAuctionMsgService(ProtocolIntf protocol, String hash) {
        _protocol = protocol;
        _model = _protocol.getHostModel();
        _hash = hash;
    }

    public void execute() throws Exception {
        EnterAuctionMsg enterAuctionMsg = new EnterAuctionMsg(_protocol);
        try {

            enterAuctionMsg.receive();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

       

        boolean result = false;
        if (_model != null) {
            result = _model.addUserToAuction(
                    _hash, enterAuctionMsg.getItemID());
        } else {
            Utils.write("niestety model==null");
        }

        if (result) {
            new AckMsg(_protocol).send();
        } else {
            new NakMsg(_protocol, "Nie udalo sie zapisac uzytkownika do aukcji").send();
        }

    }
}
